package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;

import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R<String> orders(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功");
    }


    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(Orders::getCheckoutTime);
        orderService.page(pageInfo, lqw);
        Page<OrdersDto> resultPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, resultPage, "records");
        List<Orders> records = pageInfo.getRecords();
        ArrayList<OrdersDto> ordersDtosrecords = new ArrayList<>();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record, ordersDto);
            User user = userService.getById(BaseContext.getCurrentId());
            ordersDto.setUserName(user.getName());
            LambdaQueryWrapper<OrderDetail> delqw = new LambdaQueryWrapper<>();
            delqw.eq(OrderDetail::getOrderId, ordersDto.getId());
            List<OrderDetail> list1 = orderDetailService.list(delqw);
            ordersDto.setOrderDetails(list1);
            Integer a = 0;
            for (OrderDetail orderDetail : list1) {
                Integer number = orderDetail.getNumber();
                a += number;
            }
            ordersDto.setNumber(String.valueOf(a));
            ordersDtosrecords.add(ordersDto);
        }
        resultPage.setRecords(ordersDtosrecords);
        return R.success(resultPage);
    }

    @GetMapping("/page")
    public R<Page> pageR(int page, int pageSize, String number,String beginTime, String endTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(number),Orders::getNumber, number);
        lqw.lt(endTime!=null, Orders::getCheckoutTime,endTime);
        lqw.gt(beginTime!=null,Orders::getOrderTime,beginTime);
        List<Orders> list = orderService.list(lqw);
        pageInfo.setRecords(list);
        pageInfo.setTotal(list.size());
       orderService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> status(@RequestBody Orders orders) {
        Long id = orders.getId();
        Integer status = orders.getStatus();
        Orders byId = orderService.getById(id);
        byId.setStatus(status);
        orderService.updateById(byId);

        return R.success("");
    }
    @PostMapping("/again")
    public R<List<ShoppingCart>> again(@RequestBody Orders orders){

        Long id = orders.getId();
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> list = orderDetailService.list(lqw);
        ArrayList<ShoppingCart> shopCartlist = new ArrayList<>();
        for (OrderDetail orderDetail : list) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shopCartlist.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shopCartlist);

        return R.success(shopCartlist);
    }

}